<?php

namespace App\Jobs;

use App\Models\CdrAddtion;
use App\Models\PhoneRecord;
use App\Services\SipService;
use App\Services\TwilioService;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use App\Models\RecordFileInfo;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

class SyncPhoneRecord implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $record;
    protected $sipService;

    public function __construct(PhoneRecord $phoneRecord ,$delay)
    {
        $this->record = $phoneRecord;
        $this->sipService = new SipService();
        // 设置延迟的时间，delay() 方法的参数代表多少秒之后执行
        $this->delay($delay);
        $this->onQueue('sip_record');

    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $user_phone = $this->record->user_phone;
        $fs_user_phone = $this->record->fs_user_phone;
        $create_time = $this->record->created_at;
        //SIP通话记录
        $crd_record = CdrAddtion::where([
            ['caller_id_number','=',$user_phone],
            ['destination_number','=',$fs_user_phone],
            ['start_stamp','>=',$create_time],
        ])->orderBy('start_stamp','ASC')->first();
        Log::info('SIP通话记录',['a'=>$crd_record]);

        if($crd_record){
            //where条件录音文件的名称
            $file_name = $this->sipService->cdrFileName($crd_record);

            //录音文件记录
            $tb_record_file_info = $this->sipService->searchCdrRecord($file_name);

            $recording_url = '';
            $voice_url = '';
            //录音文件存在则去下载
            if($tb_record_file_info){
                $recording_url = sipRecrodUrl($tb_record_file_info->oss_path); //http://www.tdcall.net/2019-02-19/2019-02-19-21-38-45_928618911392274_172.31.194.139_d001s002.zip
                $voice_url = $this->sipService->upload($recording_url,$tb_record_file_info->file_name); //本地的录音地址
                $this->sipService->deleteZip($tb_record_file_info->file_name);
                Log::info('本地录音地址',['msg'=>$voice_url]);
            }

            //更新本地通话记录
            $this->record->update([
                'call_status' => $crd_record['status']==1?'completed':'cancel',
                'recording_url' => $recording_url,
                'recording_time' => $crd_record['billsec'],
                'updated_at' => $crd_record['end_stamp'],
                'voice_url' => $voice_url,
            ]);

            //统计任务
            $arr = [
                'fs_user_id' => $this->record->fs_user_id,
                'task_id' => $this->record->task_id
            ];
            $this->sipService->taskStatus($arr);

            //删除掉垃圾记录
            PhoneRecord::where([
                ['call_status','=',null],
                ['apps','=',null],
                ['call_sid','=',null],
                ['created_at','<',$create_time]
            ])->delete();
        }else{
            throw new \Exception('没有对应数据,之后重复执行');
        }
    }
}
